package com.ncs.stm;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  题目修改界面
 */
public class SelectedTips extends Activity {

    public static Map<String,Map<String,List<Map<String,Object>>>> userSurveyResultMap = new HashMap<String, Map<String, List<Map<String, Object>>>>();
    private List<Map<String,Object>> surveyResultList = new ArrayList<Map<String, Object>>();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selectedtips);

        //用相应的题目选择结果加载ListView
        String userid =  getIntent().getStringExtra("userid");
        String surveyResultId =  getIntent().getStringExtra("surveyResultId");


        if (userSurveyResultMap.get(userid) != null) {
            Map<String, List<Map<String, Object>>> surveyResultMap = userSurveyResultMap.get(userid);
            if (surveyResultMap.get(surveyResultId) != null) {
                surveyResultList = surveyResultMap.get(surveyResultId);
            }
        }
        ListView tipsListView = (ListView)findViewById(R.id.selectedtip_listview);

        //todo : 需要根据最终显示UI修改显示布局方式
        SimpleAdapter simpleAdapter = new SimpleAdapter(this,surveyResultList,R.layout.tipsitem,
                new String[]{"tip_desc"},new int[]{R.id.tip_desc});

        tipsListView.setAdapter(simpleAdapter);

        tipsListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                SimpleAdapter adapter = (SimpleAdapter) adapterView.getAdapter();
                //将选择的题目重新加入到题目列表中
                Tips.tipsRemain.add((HashMap)adapterView.getItemAtPosition(i));

                //将选择的题目从选择结果中删除
                surveyResultList.remove(i);
                adapter.notifyDataSetChanged();
                return true;
            }
        });
    }

    public void backToSurvey(View view){
        Intent intent = new Intent(this,SurveyTips.class);
        startActivity(intent);
    }

    /**
     *
     * @param surveyResultId
     * @return
     */
    public static int getSelectTipsCount(String userid,String surveyResultId){

        if(userSurveyResultMap.get(userid) == null){
            return 0;
        }else{
            List tempList = userSurveyResultMap.get(userid).get(surveyResultId);
            return tempList != null ? tempList.size() : 0;
        }
    }
}